<template>
  <div>
    <div style="margin-left: 0px">
      <!--      <span v-if="docuData != null && docuData.originName != null">{{docuData.originName}}</span>-->
      <!--      <el-button type="text" @click="fileDownLoad">下载</el-button>-->

      <div
        v-if="docuData != null && docuData.originName != null"
        style="margin-left: 0px"
      >
        <i class="el-icon-document"></i>
        <el-button type="text" @click="fileDownLoad">
          {{ docuData.originName }}
          <!--          <span v-if="cardForm.version != null">({{cardForm.version}})</span>-->
        </el-button>
        <el-button type="text" @click="fileDownLoad">下载</el-button>
      </div>
    </div>
  </div>
</template>

<script>
  import { getAction, downloadAction } from '@/api/action'

  export default {
    name: 'FileList',
    props: {
      workFlowOid: {
        type: String,
        default: '',
      },
    },
    data() {
      return {
        fileList: [],
        url: {
          valutList: '/presoftasim/wf/document/list',
          downloadUrl: '/presoftasim/common/downloadFile',
        },
        docuData: {},
      }
    },
    watch: {
      workFlowOid: function (newValue, oldValue) {
        if (newValue !== '' && newValue !== undefined) {
          this.getDoumentByOid(newValue)
        }
      },
    },
    created() {
      this.getDoumentByOid(this.workFlowOid)
    },
    methods: {
      getDoumentByOid(oid) {
        getAction(this.url.valutList, { oid: oid })
          .then((response) => {
            if (response.code === 200) {
              this.docuData = response.data
            } else {
              this.submitFail(response.message)
            }
          })
          .catch((err) => {
            console.log(err)
          })
      },
      fileDownLoad() {
        downloadAction(this.url.downloadUrl, {
          filePath: this.docuData.diskPath,
        }).then((res) => {
          if (!res) {
            this.$message.warning('文件下载失败')
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(
              new Blob([data], { type: 'application/vnd.ms-excel' }),
              this.docuData.originName
            )
          } else {
            let url = window.URL.createObjectURL(
              new Blob([res], { type: 'application/vnd.ms-excel' })
            )
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', this.docuData.originName)
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
          }
        })
      },
    },
  }
</script>
